# Copyright (c) 2010 Xynilex Project
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

cmake_minimum_required(VERSION 2.6)

project(Xynilex)

set(Xynilex_VERSION_MAJOR 0)
set(Xynilex_VERSION_MINOR 1)
set(Xynilex_VERSION_REV 1)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}
  "${CMAKE_SOURCE_DIR}/CMake"
  "${CMAKE_SOURCE_DIR}/CMake/Packages"
  "${CMAKE_SOURCE_DIR}/CMake/Utils"
)

include(CopyFile)

set(Xynilex_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/Bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${Xynilex_OUTPUT_DIRECTORY})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${Xynilex_OUTPUT_DIRECTORY})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${Xynilex_OUTPUT_DIRECTORY})
link_directories(${Xynilex_OUTPUT_DIRECTORY})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/Source)

# ----------( Configuration )----------

option(XYNILEX_BUILD_CLIENT "Build the XynilexClient" TRUE)
option(XYNILEX_BUILD_SERVER "Build the XynilexServer" TRUE)
option(XYNILEX_BUILD_TESTS "Build Xynilex Tests" FALSE)

# -------------------------------------

set(Xynilex_DEPENDENCY_NAMES "")
macro(IncludePackage name)
  find_package(${name} REQUIRED)
  include_directories(${${name}_INCLUDE_DIRS})

  list(LENGTH ${name}_SHARED_LIBRARIES ${name}_SHARED_LIBRARIES_LEN)

  if(NOT ${name}_SHARED_LIBRARIES_LEN EQUAL 0)
    copy_files("${${name}_SHARED_LIBRARIES}" ${Xynilex_OUTPUT_DIRECTORY})
  endif()

  set(Xynilex_DEPENDENCY_NAMES ${Xynilex_DEPENDENCY_NAMES} ${name})
endmacro()

macro(FinishDependencyChecking)
  set(Xynilex_MISSING_DEPENDENCIES "")

  foreach(name ${Xynilex_DEPENDENCY_NAMES})
    if(NOT ${name}_FOUND)
      set(Xynilex_MISSING_DEPENDENCIES ${Xynilex_MISSING_DEPENDENCIES} ${name})
    endif()
  endforeach()

  list(LENGTH Xynilex_MISSING_DEPENDENCIES Xynilex_MISSING_DEPENDENCIES_LEN)

  if(Xynilex_MISSING_DEPENDENCIES_LEN EQUAL 0)
    message(STATUS "")
    message(STATUS "All dependencies are met.")
    message(STATUS "")
  else()
    message(ERROR "")
    message(ERROR " The following dependencies are missing:")

    foreach(name ${Xynilex_MISSING_DEPENDENCIES})
      message(ERROR " - ${name}")
    endforeach()

    message(ERROR "")
  endif()
endmacro()

include_directories("${CMAKE_CURRENT_SOURCE_DIR}/Libs/boost")

add_subdirectory(Source)
add_subdirectory(Tests)
